package com.zrrd.mapperTest;

import com.github.pagehelper.PageHelper;
import com.zrrd.mapper.TeaMapper;
import com.zrrd.pojo.Tea;
import com.zrrd.utils.DBUtils;
import com.zrrd.utils.PageBean;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title TeaMapperTest_utli
 * @description
 * @create 2024/1/23 10:58
 */
public class TeaMapperTest_utli {
    @Test
    public void testSelectAllTea(){
        SqlSession sqlSession = DBUtils.getSession();
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        System.out.println(mapper);
        List<Tea> teas = mapper.queryTeaAll();
        System.out.println(teas);
        sqlSession.close();
    }


    @Test
    public void queryOneByIdTest(){
        //调用工具类，获取sqlSession
        SqlSession sqlSession = DBUtils.getSession();
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        Tea tea = mapper.queryOneById(1);
        System.out.println(tea);
        sqlSession.close();
    }

    @Test
    public void insertTeaTest(){
        //调用工具类，获取sqlSession
        SqlSession sqlSession = DBUtils.getSession(true);
        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);
        Integer count = mapper.insertTea(new Tea(null, "冰红茶", 1, 100, "红茶红茶红茶红茶", 0));

        System.out.println(count>0?"插入成功":"插入失败");
        sqlSession.close();
    }

    @Test
    public void updateTeaTest(){
        //调用工具类，获取sqlSession
        SqlSession sqlSession = DBUtils.getSession(true);

        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);

        Integer count = mapper.updateTea(new Tea(24, "青梅绿茶", 1, 100, "红茶红茶红茶红茶", 0));
        //提交事务，什么时候需要提交事务？增删改，出现改动的时候。
        sqlSession.commit();

        System.out.println(count>0?"修改成功":"修改失败");
        sqlSession.close();
    }


    @Test
    public void deleteTeaTest(){
        //调用工具类，获取sqlSession
        SqlSession sqlSession = DBUtils.getSession(true);

        TeaMapper mapper = sqlSession.getMapper(TeaMapper.class);

        Integer count = mapper.deleteTea(25);
        //提交事务，什么时候需要提交事务？增删改，出现改动的时候。
        sqlSession.commit();

        System.out.println(count>0?"删除成功":"删除失败");
        sqlSession.close();
    }
    @Test
    public void queryByConditionTest(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        Tea tea = new Tea();
        tea.setId(3);
        tea.setTeaName("金骏眉");
        System.out.println(teaMapper.queryByCondition(tea));
    }
    @Test
    public void queryByIds(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        System.out.println(teaMapper.queryByIds(list));
    }
    @Test
    public void fuzzyQueryByNameInfoTest(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        List<Tea> teas = teaMapper.fuzzyQueryByNameInfo("%金%");
        System.out.println(teas);
    }
    @Test
    public void teaStatistics(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        List<Tea> teas = teaMapper.teaStatistics(0,10);
        System.out.println(teas);
    }
    /*
     /*
            52条数据   总条数
            每页10条   每页条数
            分6页     总页数
            页码    start end
            1  ---  1，  10
            2  ---  11， 20
            3  ---  21， 30
            4  ---  31， 40
            5  ---  41， 50
            6  ---  51， 52

            int currentPage=1; //当前页码
            int pageSize=10; //每页条数

            //int total=52;   //总条数
            //int totalPage=6; //总页数

            int start=1;    //从1开始
            int end=10;     //到10结束

            start=(currentPage-1)*pageSize+1;   //1 11      41  51
            end=start+pageSize-1;               //10 20     50  60


     */
    @Test
    public void queryByPageTest(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        //查询所有
        List<Tea> teas = teaMapper.queryTeaAll();
        int currentPage=4; //当前页码
        int pageSize=10; //每页条数
        int start=(currentPage-1)*pageSize;
        int end=currentPage*pageSize;

        //新建一个集合，用来临时存每页的数据
        ArrayList<Tea> tempList = new ArrayList<>();

        int size=(start+pageSize)>=teas.size()?teas.size():(start+pageSize);

        for (int i = start; i < size; i++) {
            Tea tea = teas.get(i);
            tempList.add(tea);
        }
        for (Tea t:tempList) {
            System.out.println(t);
        }
    }

    /**
     * 分页查询单元测试
     */
    @Test
    public void queryByPageTest02(){
        //PageBean<Tea> teaPageBean = queryByPage(2, 10);
        //List<Tea> rows = teaPageBean.getRows();
        List<Tea> rows = queryByPage02(2, 10);

        for (Tea t:rows) {
            System.out.println(t);
        }
    }

    /**
     * 使用PageHelper插件完成分页
     */
    @Test
    public void queryByPageTest03(){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);
        //设置分页参数
        PageHelper.startPage(2,10);
        List<Tea> teas = teaMapper.queryTeaAll();
        for (Tea t:teas) {
            System.out.println(t);
        }

    }







    /**
     * 通过自己创建PageBan类，封装分页后的数据内容包括：总条数和每页数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    public static PageBean<Tea> queryByPage(int currentPage, int pageSize){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);

        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        //查询到当前页数据
        List<Tea> rows = teaMapper.queryByPage(begin,size);

        //查询总记录数
        int totalCount=teaMapper.queryTotalCount();

        PageBean<Tea> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        return pageBean;
    }

    /**
     * 通过Mybatis提供的RowBounds类，完成逻辑分页
     * @param currentPage
     * @param pageSize
     * @return
     */
    public static List<Tea> queryByPage02(int currentPage, int pageSize){
        TeaMapper teaMapper = DBUtils.getSession().getMapper(TeaMapper.class);

        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        //查询到当前页数据
        RowBounds rowBounds=new RowBounds(begin,size);
        List<Tea> rows = teaMapper.queryByPage02(rowBounds);

        return rows;
    }
}
